package com.ydy.rod;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.davidfantasy.jwtshiro.JWTHelper;
import com.github.davidfantasy.jwtshiro.annotation.AlowAnonymous;
import com.ydy.domain.ScoreInfo;
import com.ydy.domain.Student;
import com.ydy.service.StudentBiz;
import com.ydy.service.StudentService;
import com.ydy.vo.CommonConstants;
import com.ydy.vo.JsonResult;
import com.ydy.vo.Studentvo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/Student")
public class ScorelnfoController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentBiz studentBiz;
    @Autowired
    private JWTHelper jwtHelper;
    private final StringRedisTemplate stringRedisTemplate;
    @AlowAnonymous
    @PostMapping("/addStudent")
    public JsonResult addStudent(@RequestBody Studentvo studentvo, HttpServletRequest request) {
        if (StringUtils.isEmpty(studentvo.getName())) {
            return JsonResult.fail("用户名不能为空");
        }
        if (StringUtils.isEmpty(studentvo.getPassword())) {
            return JsonResult.fail("密码不能为空");
        }
        if (StringUtils.isEmpty(studentvo.getCnName())) {
            return JsonResult.fail("中文姓名不能为空");
        }
        String remoteAddr = request.getRemoteAddr();
        log.info("请求用户ip:{}", remoteAddr);
        studentvo.setIp(remoteAddr);
        return studentBiz.addStudentInfo(studentvo);
    } @AlowAnonymous
    @GetMapping("/login")
    public JsonResult login(String name, String password, @RequestParam(required = false) Integer refresh) {
        Studentvo vo = studentBiz.checkAccount(name, password);
        if (refresh != null && refresh == 1) {
            return JsonResult.suc(vo);
        }
        //查询用户是否有生效的token
        String s = stringRedisTemplate.opsForValue().get(CommonConstants.rangel_redis_key + CommonConstants.student_redis_key + vo.getId());
        if (!StringUtils.isEmpty(s)) {
            return JsonResult.suc(s);
        }
        //redis无缓存的token
        String token =jwtHelper.sign(name,password);

        DecodedJWT jwt = this.jwtHelper.verify(token, name, password);
        log.info("登录成功:{}",jwt);
        stringRedisTemplate.opsForValue().set(CommonConstants.rangel_redis_key+CommonConstants.student_redis_key+vo.getId(), token,360, TimeUnit.MINUTES);
        //redis->rangel/user/token
        stringRedisTemplate.opsForValue().set(CommonConstants.rangel_redis_key+CommonConstants.student_redis_key+token, JSON.toJSONString(vo),360, TimeUnit.MINUTES);
        return JsonResult.suc(token);
    }
    @GetMapping("/getUserInfo")
    public Studentvo getUserInfo(ServletRequest request) {
        return studentBiz.getStudentInfo(request);
    }


    /**投票
     *必传参数 分数,目标用户名(姓名拼音)
     * @param scoreInfo
     * @param request
     * @return*/
    @PostMapping("/vote")
    public JsonResult vote(@RequestBody ScoreInfo scoreInfo, HttpServletRequest request){
        return studentBiz.vote(scoreInfo,request);
    }

    @GetMapping("/findAll")
    public JsonResult findAll(){
        return JsonResult.suc(studentBiz.findAllScoreInfo());
    }

//    @GetMapping("/getStudentByscore")
//    public Student getStudentByscore(Integer score){
//        return studentService.;
//    }

}
